package com.lijie.ljblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijie.ljblog.mapper.LiuyanMapper;
import com.lijie.ljblog.mapper.ReplayMapper;
import com.lijie.ljblog.model.Liuyan;
import com.lijie.ljblog.model.Mail;
import com.lijie.ljblog.model.Person;
import com.lijie.ljblog.model.Replay;
import com.lijie.ljblog.service.LiuyanService;
import com.lijie.ljblog.service.MailService;
import com.lijie.ljblog.service.UtilService;
import com.lijie.ljblog.util.QueryWrapperUtil;
import com.lijie.ljblog.util.Result;
import com.lijie.ljblog.util.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lijie
 * @since 2021-08-28
 */
@Service
@Slf4j
public class LiuyanServiceImpl extends ServiceImpl<LiuyanMapper, Liuyan> implements LiuyanService {
    @Autowired
    LiuyanMapper liuyanMapper;
    @Autowired
    QueryWrapperUtil queryWrapperUtil;
    @Autowired
    UtilService utilService;
    @Autowired
    MailService mailService;
    @Value("${lijieEmail}")
    String ljEmail;
    @Autowired
    ReplayMapper replayMapper;
    @Autowired
    ExecutorService executorService;


    @Override
    public PageInfo<Liuyan> getLiuyanList(Integer page, Liuyan liuyan) {
        PageHelper.startPage(page,50);
        List<Liuyan> liuyans = liuyanMapper.getLiuyanList(liuyan,utilService.getFileHeadPath());
        for(Liuyan liuyan1:liuyans){
            List<Replay> replayListById = replayMapper.getReplayListById(liuyan1.getId(), utilService.getFileHeadPath());
            liuyan1.setReplays(replayListById);
        }
        return new PageInfo<>(liuyans);
    }

    @Override
    public Boolean commitLiuyan(Liuyan liuyan, HttpServletRequest request) {
        String ip = WebUtils.getIpByRequset(request);
        liuyan.setIp(ip);
        Result<Map<String, Object>> locationByIP = utilService.getLocationByIP(ip);
        if(locationByIP.isSuccess()){
            if(liuyan.getBlogid()!=null&&liuyan.getPersonId()!=null){
                Map<String,Object> data=locationByIP.getData();
                String address = MapUtils.getString(data, "address");
                String jd = MapUtils.getString(data, "jd");
                String wd = MapUtils.getString(data, "wd");
                liuyan.setLocation(address);
                liuyan.setJd(jd);
                liuyan.setWd(wd);
            }
        }
        int insert = liuyanMapper.insert(liuyan);
        if(insert>0){
            Mail mail=new Mail();
            mail.setEmail(ljEmail);//留言的收件人只会是我自己
            Map<String,Object> map=new HashMap<>();
            map.put("code",liuyan.getMessage());//模板的数据
            mail.setAttachment(map);
            mail.setTitle("什么是快乐 有新的留言");
            //之前是用的异步注解

            //使用线程池
            executorService.submit(()-> {
                log.info("进线程池----->>>");
                mailService.sendTemplateMail(mail, "replayMyselfEmail");
            });
            //测试多线程
//            MailServiceThreadImpl replayMyselfEmail = new MailServiceThreadImpl(mail, "replayMyselfEmail");
//            Thread mailSendThread=new Thread(replayMyselfEmail);
//            mailSendThread.start();
        }
        return insert>0;
    }

    @Override
    public Result<Integer> getLiuyanNums() throws Exception{
        Result<Integer> result=new Result<>();
        QueryWrapper<Liuyan> queryWrapper=new QueryWrapper<>();
        Integer integer = liuyanMapper.selectCount(queryWrapper);
        result.setData(integer);
        return result;
    }

    public static void main(String[] args) {
        List<Person> a=new ArrayList<>();
        Person person = new Person();
        person.setUserName("李杰");
        a.add(person);
        person.setUserName("zzl");
        System.out.println(a);
    }
}
